home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / croutes.zip / READS.C < prev    next >
Text File  |  1980-01-01  |  2KB  |  78 lines

  1. /*                          *** reads.c ***                          */
  2. /*                                                                   */
  3. /* IBM - PC microsoft "C"                                            */
  4. /*                                                                   */
  5. /* Function to read a string from stdin.  Returns the number of      */
  6. /* characters entered including the NULL byte.                       */
  7. /*                                                                   */
  8. /* Written by L. Cuthbertson, March 1984.                            */
  9. /*                                                                   */
  10. /*********************************************************************/
  11. /*                                                                   */
  12.  
  13. #define NULL '\000'
  14. #define CONC '\003'
  15. #define BELL '\007'
  16. #define CONX '\030'
  17. #define ESC '\033'
  18. #define DEL '\177'
  19.  
  20. int reads(s,maxlen)
  21. char s[];
  22. int maxlen;
  23. {
  24.     int len,c,readc(),writec();
  25.  
  26.     /* acceptance loop */
  27.     for (len=1;((c=readc()) != '\r') && (c != '\n');len++) {
  28.  
  29.         /* handle backspaces and deletes */
  30.         if ((c == '\b') || (c == DEL)) {
  31.             if (len > 1) {
  32.                 writec('\b');
  33.                 writec(' ');
  34.                 writec('\b');
  35.                 len -= 2;
  36.             } else {
  37.                 len = 0;
  38.             }
  39.  
  40.         /* handle escape and control-z */
  41.         } else if ((c == ESC) || (c == CONX) || (c < 0)) {
  42.             while (len > 1) {
  43.                 writec('\b');
  44.                 writec(' ');
  45.                 writec('\b');
  46.                 len--;
  47.             }
  48.             len = 0;
  49.  
  50.         /* handle control-c */
  51.         } else if (c == CONC) {
  52.             exit();
  53.  
  54.         /* ring their bell if length exceeded */
  55.         } else if (len >= maxlen) {
  56.             writec (BELL);
  57.             len--;
  58.  
  59.         /* handle strange control characters */
  60.         } else if (c < ' ') {
  61.             writec (BELL);
  62.             len--;
  63.  
  64.         /* got a valid character */
  65.         } else {
  66.             s[len-1] = c;
  67.             writec(c);
  68.         }
  69.  
  70.     }
  71.  
  72.     /* add string terminator */
  73.     s[len-1] = NULL;
  74.  
  75.     /* return total length of string entered */
  76.     return (len);
  77. }
  78.